package com.mc.moring.widge

import android.content.Context
import android.util.AttributeSet
import android.util.Log
import android.view.MotionEvent
import android.widget.RelativeLayout
import com.mc.moring.util.ScreenUtil
import kotlin.math.abs


class CustomDragView @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : RelativeLayout(context, attrs, defStyleAttr) {

    private var leftClickListener: OnClickListener? = null
    private var rightClickListener: OnClickListener? = null
    private var mWidth = 0
    private var mHeight = 0
    private var screenWidth = 0
    private var screenHeight = 0

    private var downX = 0f
    private var downY = 0f

    //是否拖动
    var isDrag = false

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        mWidth = measuredWidth
        mHeight = measuredHeight
        screenWidth = ScreenUtil.getScreenWidth(context)
        screenHeight = ScreenUtil.getScreenHeight(context)
    }



    override fun onTouchEvent(event: MotionEvent): Boolean {
        if (this.isEnabled) {
            when (event.action) {
                //DOWN时，即刚开始的触摸点相对view的坐标。
                MotionEvent.ACTION_DOWN -> {
                    isDrag = false
                    downX = event.x
                    downY = event.y
                }
                MotionEvent.ACTION_MOVE -> {
                    //滑动的距离 = 触摸点滑动到的坐标 - 开始触摸的坐标 （都是相对于view本身）
                    val xDistance = event.x - downX
//                    //可能不需要拖动入口水平坐标
//                    val xDistance = 0
                    val yDistance = event.y - downY
                    var l: Int
                    var r: Int
                    var t: Int
                    var b: Int
                    //当水平或者垂直滑动距离大于10,才算拖动事件
                    if (abs(xDistance) > 10 || abs(yDistance) > 10) {
                        isDrag = true
                        l = (left + xDistance).toInt()
                        r = l + mWidth
                        t = (top + yDistance).toInt()
                        b = t + mHeight
                        //不划出边界判断,此处应按照项目实际情况,因为本项目需求移动的位置是手机全屏,
                        // 所以才能这么写,如果是固定区域,要得到父控件的宽高位置后再做处理
//                        if (l < 0) {
//                            Log.e("----l < 0","true")
//                            l = 0
//                            r = l + mWidth
//                        } else
                        if (r > screenWidth) {
                            Log.e("----r > screenWidth","true")
                            r = screenWidth
                            l = r - mWidth
                        }
//                        if (t < 0) {
//                            Log.e("----t < 0","true")
//                            t = 0
//                            b = t + mHeight
//                        } else
                        if (b > screenHeight) {
                            Log.e("----b > screenHeight","true")
                            b = screenHeight - mHeight
                            t = b - mHeight
                        }
                        layout(l, t, r, b)
                    }
                }

                MotionEvent.ACTION_UP -> {
                    // 这里宽度必须确定宽高
                    val params = LayoutParams(mWidth, mHeight)
                    params.setMargins(left, top, 0, 0)
                    layoutParams = params
                    isPressed = false

                    if ( !isDrag ){
                        val x = event.x
                        val y = event.y
                        if ( abs(x-downX)<10 && abs(y-downY)<10 ){
                            if ( (x < (right-left)*5/6) || y > (bottom-top)/6) {
                                if (leftClickListener != null) {
                                    leftClickListener!!.onClick(this)
                                }
                            } else {
                                if (rightClickListener != null) {
                                    rightClickListener!!.onClick(this)
                                }
                            }
                        }
                    }
                }
                MotionEvent.ACTION_CANCEL -> isPressed = false

            }
            return true
        }
        return false
    }

    fun setLeftClickListener(listener: OnClickListener) {
        leftClickListener = listener
    }

    fun setRightClickListener(listener: OnClickListener) {
        rightClickListener = listener
    }

}
